dojo.provide("uws.Drawing");
dojo.require("uws.Tree");

dojo.require("dojox.gfx.VectorText");
dojo.require('uws.drawing.SurfaceManager');

var connectionHandler, componentManager, connectionManager, componentFocusManager, drawManager, propertiesConstants, propertiesManager, surfaceManager;
var childrenManager, connectionRestrictionsConstants, propertiesConstants, sortOrderManager;
var defaultFont = {size:12,family:"helvetica,arial,sans-serif"};
var vf = new dojox.gfx.VectorFont(dojo.moduleUrl("dojox", "gfx/resources/Gillius.svg"));

//the function that updates a component's x and y coordinates on the data store everytime it moves
function shapeMoveHandler(mover){
	if (mover.shape.matrix == null) {
		mover.shape.matrix = {
			dx: 0,
			dy: 0
		};
	}
	
	if(dataStore==null) return;
	
	dataStore.fetchItemByIdentity({
		identity:mover.shape.owningUWSComp.id,
		scope:this,
		onItem:function(item){
			try{
				if(item!=null){
					dataStore.setValue(item, 'x', mover.shape.matrix.dx);
					dataStore.setValue(item, 'y', mover.shape.matrix.dy);
				}
			}catch(e){
				console.debug(e);
			}
		}
	});
};

function prepareDrawingModule(){
	surfaceManager = new uws.drawing.SurfaceManager(root.diag_counter[0]);
	
	dojox.gfx.Shape.extend({
		owningUWSComp:null
	});
	
	dojox.gfx.Surface.extend({
		//from the data store
		id:null,
		name:null,
		type:null,
		project_id:null,
		
		//references
		containerId:null,
		containerTabId:null,
		
		//for the connector
		uws_components:[]
	});
	
	dojox.gfx.Text.prototype.getBoundingBox = function(){ 
		try{
			return this.rawNode.getBBox();
		}catch(e){
			return null;
		}
	};
	dojox.gfx.Group.prototype.getBoundingBox = function(){
		try{
			return this.rawNode.getBBox();
		}catch(e){
			return null;
		}
	};

	dojox.gfx.defaultStroke = {type: "stroke", color: "black", style: "solid", width: 1, cap: "butt", join: 4};
	console.debug("Renderer: "+dojox.gfx.renderer);
	
	childrenManager = new uws.drawing.ChildrenManager(root.comp_children_counter[0]);
	connectionHandler = new uws.drawing.ConnectionHandler();
	connectionManager = new uws.drawing.ConnectionManager(root.conn_counter[0]);
	connectionRestrictionsConstants = new uws.drawing.ConnectionRestrictionsConstants();
	componentManager = new uws.drawing.ComponentManager(root.comp_counter[0]);
	componentFocusManager = new uws.drawing.ComponentFocusManager();
	drawManager = new uws.drawing.DrawManager();
	propertiesConstants = new uws.drawing.PropertiesConstants();
	propertiesManager = new uws.drawing.PropertiesManager();
	sortOrderManager = new uws.drawing.SortOrderManager();
}

function loadDrawingModule(fxn){
	dojo.require("dojox.gfx.Moveable");
	dojo.require("dojox.gfx.utils");
	
	dojo.require('uws.drawing.ChildrenManager');
	dojo.require('uws.drawing.ComponentFocusManager');
	dojo.require('uws.drawing.ComponentManager');
	dojo.require('uws.drawing.ConnectionHandler');
	dojo.require('uws.drawing.ConnectionManager');
	dojo.require('uws.drawing.ConnectionRestrictionsConstants');
	dojo.require('uws.drawing.DrawManager');
	dojo.require('uws.drawing.PropertiesConstants');
	dojo.require('uws.drawing.PropertiesManager');
	dojo.require('uws.drawing.SortOrderManager');
	
	dojo.require('uws.drawing.activity.Activity');
	dojo.require('uws.drawing.activity.Decision');
	dojo.require('uws.drawing.activity.Start');
	dojo.require('uws.drawing.activity.Stop');
	dojo.require('uws.drawing.activity.Flow');
	
	dojo.require('uws.drawing.classdiagram.ClassBox');
	dojo.require('uws.drawing.classdiagram.Generalization');
	
	dojo.require('uws.drawing.sequence.ObjectComponent');
	dojo.require('uws.drawing.sequence.Loop');
	dojo.require('uws.drawing.sequence.Flow');
	
	dojo.require('uws.drawing.sqlerd.Entity');
	dojo.require('uws.drawing.sqlerd.WeakEntity');
	dojo.require('uws.drawing.sqlerd.Attribute');
	dojo.require('uws.drawing.sqlerd.KeyAttribute');
	dojo.require('uws.drawing.sqlerd.DerivedAttribute');
	dojo.require('uws.drawing.sqlerd.MultivaluedAttribute');
	dojo.require('uws.drawing.sqlerd.Connection');
	dojo.require('uws.drawing.sqlerd.Relationship');
	dojo.require('uws.drawing.sqlerd.IdentifyingRelationship');
	
	dojo.require('uws.drawing.usecase.Actor');
	dojo.require('uws.drawing.usecase.Note');
	dojo.require('uws.drawing.usecase.UseCase');
	dojo.require('uws.drawing.usecase.SystemBox');
	dojo.require('uws.drawing.usecase.Association');
	dojo.require('uws.drawing.usecase.Dependency');
	dojo.require('uws.drawing.usecase.Generalization');
	
	if(fxn){
		dojo.addOnLoad(fxn);
	}
}

function saveAsSVG(surface){	
	var svg = dojox.gfx.utils.toSvg(surface);
	svg.addCallback(function(content){
		openWindowWithPost("ajax/download_svg.php", "Download as SVG", ["content"], [content])
	});
	svg.addErrback(function(content){
		alert(content);
	});
}

